<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
include( '../overview/task_functions.php' );

//global $dal;

$statement = $dal->prepare( 'SELECT * ' .
		'FROM ruler_task ' .
		'JOIN tasks ON tasks.task_id = ruler_task.task_id ' .
		'WHERE ruler_id = :ruler_id AND completed = 0' );
$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
$task = $dal->getObject( $statement );

if( $task->finished == 1 ) {
	completeTask( $task );
}
elseif ( $task->next_task2 != -1 ) {
	// Multiple choice, let's finalize it!
	if( is_numeric( $_POST[ 'next_task' ] ) && 
		( $_POST[ 'next_task' ] == $task->next_task || $_POST[ 'next_task' ] == $task->next_task2 || $_POST[ 'next_task' ] == $task->next_task3 ) ) {
		completeTask( $task, $_POST[ 'next_task' ] );
	}
	else {
		$status = translate( 'Please, make a choice and confirm.' );
	}
}
elseif( $task->required_capital == 1 ) {
	if( isset( $_POST[ 'capital_x' ] ) && is_numeric( $_POST[ 'capital_x' ] ) 
		&& isset( $_POST[ 'capital_y' ] ) && is_numeric( $_POST[ 'capital_y' ] ) ) {
		$statement = $dal->prepare( 'SELECT x, y ' .
				'FROM village ' .
				'WHERE start_place = 1 AND ruler_id = :ruler_id' );
		$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
		$coordinates = $dal->getObject( $statement );
		
		if( $coordinates->x == $_POST[ 'capital_x' ] && $coordinates->y == $_POST[ 'capital_y' ] ) {
			completeTask( $task );
		}
		else {
			$status = translate( 'The supplied coordinates are not correct, try again.' );
		}
	}
	else {
		$status = translate( 'You have to put in the x and y coordinate of your capitals location.' );
	}
}
else {
	$status = translate( 'Something has gone wrong, please contact an administrator.' );
}
if( isset( $status ) ) {
?>
			<strong><?php transprint( 'Status' ); ?></strong>: <?php print( $status ); ?><br />
			<br />
<?php	
}

$completed_all = false;

$query = "SELECT * " .
		"FROM ruler_task " .
		"JOIN tasks ON tasks.task_id = ruler_task.task_id " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND completed = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );

if( !isset( $row[ 'task_id' ] ) ) {
	$completed_all = true;
}

if( $completed_all || !isset( $row[ 'task_id' ] ) ) {
	printMedalProgress();	
}
else {
	printCurrentTask();
}
?>
